package com.xjh.scheduleTask;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xjh.t.domain.Order;
import com.xjh.t.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Component
public class ScheduledTasks {

    @Autowired
    private IOrderService orderService;

    @Scheduled(cron = "0 * * * * ?") // 每分钟执行一次
    public void performTaskWithCronExpression() {
       // System.out.println("Perform task with cron expression: " + System.currentTimeMillis());

        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_state",0);
        queryWrapper.eq("over_time",0);

        List<Order> orderList = orderService.list(queryWrapper);
        Date curtime = new Date();
        for(Order order:orderList)
        {
           Long diff = curtime.getTime() - order.getCreateTime().getTime();

           if(diff/1000 > 3600){

               order.setOverTime(1);
               order.setOrderState(3);

               orderService.updateById(order);
           }
        }
    }

}
